package com.maizhi.service.impl;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.maizhi.model.Files;
import com.maizhi.dao.hrDaoI;
import com.maizhi.hr.model.HrLwImp;
import com.maizhi.hr.model.HrUserInfo;
import com.maizhi.hr.model.HrWbImp;
import com.maizhi.hr.model.HrXjInfo;
import com.maizhi.service.hrServiceI;
import com.maizhi.util.xlsxUtil;
import com.maizhi.util.StringUtil;

@Service("hrService")
public class hrServiceImpl extends BaseServiceImpl implements hrServiceI {
	
	Logger logger = Logger.getLogger(hrServiceImpl.class);

	hrDaoI hrDao;
	public hrDaoI getHrDao() {
		return hrDao;
	}

	@Autowired
	public void setHrDao(hrDaoI hrDao) {
		this.hrDao = hrDao;
	}

	public boolean lwBak() {
		// TODO Auto-generated method stub
		return false;
	}

	@Transactional 
	public boolean lwClear() {
		boolean flag=false;
		String hql="DELETE FROM HrLwImp";
		hrDao.executeHql(hql);
		flag=true;
		return flag;
	}

	@CacheEvict(value = "hrCache", allEntries = true)
	public int lwImpl(Files file) {
		int n=0;
		boolean flag=true;
		File f=null;
		if(file!=null){
			f=new File(file.getFilePath()+"\\"+file.getFlagName());
		}
		try{
		Date logDate = new Date();
		xlsxUtil xlsxu = new xlsxUtil();
		List<List<String>> res= xlsxu.parseXlsXlsxByPoi(f);
			if(res!=null && res.size()>0){
				for(int i=0;i<res.size();i++){
					List<String> row = res.get(i);
					if(row!=null && row.size()>0){
						try{
							if(!StringUtil.nullTrim(row.get(0)).equals("")){
								HrLwImp lw = new HrLwImp();
								DecimalFormat df = new DecimalFormat("#");
								lw.setId(StringUtil.uuid());
								lw.setCreateDate(new Date());
								lw.setStatus(1);
								try{
									lw.setEmployeeId(df.format(Double.parseDouble(StringUtil.nullTrim(row.get(0)))));
								}catch(Exception e){
									lw.setEmployeeId(StringUtil.nullTrim(row.get(0)));
								}
								lw.setEmployeeName(StringUtil.nullTrim(row.get(1)));
								lw.setIsJj(Integer.parseInt(StringUtil.nullTrimDef(row.get(2),"1","0")));
								lw.setXbReward(Float.parseFloat(StringUtil.nullTrimDef(row.get(3),"0")));
								lw.setZjReward(Float.parseFloat(StringUtil.nullTrimDef(row.get(4),"0")));
								lw.setHjReward(Float.parseFloat(StringUtil.nullTrimDef(row.get(5),"0")));
								lw.setRemark(StringUtil.notNull(row.get(6)));
								lw.setLogDate(logDate);
								hrDao.save(lw);
								n++;
							}
						}catch(Exception e){
							//logger.error("数据读入异常 ： "+e.getMessage());
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error("导入劳务数据异常： "+e.getMessage());
		}
		return n;
	}

	public boolean wbBak() {
		// TODO Auto-generated method stub
		return false;
	}

	@Transactional 
	public boolean wbClear() {
		boolean flag=false;
		String hql="DELETE FROM HrWbImp";
		hrDao.executeHql(hql);
		flag=true;
		return flag;
	}

	@CacheEvict(value = "hrCache", allEntries = true) 
	public int wbImpl(Files file) {
		int n=0;
		boolean flag=true;
		File f=null;
		if(file!=null){
			f=new File(file.getFilePath()+"\\"+file.getFlagName());
		}
		try{
		Date logDate = new Date();
		xlsxUtil xlsxu = new xlsxUtil();
		List<List<String>> res= xlsxu.parseXlsXlsxByPoi(f);
			if(res!=null && res.size()>0){
				for(int i=0;i<res.size();i++){
					List<String> row = res.get(i);
					if(row!=null && row.size()>0){
						try{
							if(!StringUtil.nullTrim(row.get(0)).equals("")){
								HrWbImp lw = new HrWbImp();
								DecimalFormat df = new DecimalFormat("#");
								lw.setId(StringUtil.uuid());
								lw.setCreateDate(new Date());
								lw.setStatus(1);
								try{
									lw.setEmployeeId(df.format(Double.parseDouble(StringUtil.nullTrim(row.get(0)))));
								}catch(Exception e){
									lw.setEmployeeId(StringUtil.nullTrim(row.get(0)));
								}
								lw.setEmployeeName(StringUtil.nullTrim(row.get(1)));
								lw.setKhScore(Float.parseFloat(StringUtil.nullTrimDef(row.get(2),"0")));
								lw.setKhLevel(Float.parseFloat(StringUtil.nullTrimDef(row.get(3),"0")));
								lw.setKhPer(Float.parseFloat(StringUtil.nullTrimDef(row.get(4),"0")));
								lw.setRemark(StringUtil.notNull(row.get(5)));
								lw.setLogDate(logDate);
								hrDao.save(lw);
								n++;
							}
						}catch(Exception e){
							//logger.error("数据读入异常 ： "+e.getMessage());
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error("导入外包数据异常： "+e.getMessage());
		}
		return n;
	}

	public boolean xjBak() {
		// TODO Auto-generated method stub
		return false;
	}

	@Transactional 
	public boolean xjClear() {
		boolean flag=false;
		String hql="DELETE FROM HrXjInfo";
		hrDao.executeHql(hql);
		flag=true;
		return flag;
	}

	@CacheEvict(value = "hrCache", allEntries = true)
	public int xjImpl(Files file) {
		int n=0;
		boolean flag=true;
		File f=null;
		if(file!=null){
			f=new File(file.getFilePath()+"\\"+file.getFlagName());
		}
		try{
		Date logDate = new Date();
		xlsxUtil xlsxu = new xlsxUtil();
		List<List<String>> res= xlsxu.parseXlsXlsxByPoi(f);
			if(res!=null && res.size()>0){
				for(int i=0;i<res.size();i++){
					List<String> row = res.get(i);
					if(row!=null && row.size()>0){
						try{
							if(!StringUtil.nullTrim(row.get(0)).equals("")){
								HrXjInfo lw = new HrXjInfo();
								DecimalFormat df = new DecimalFormat("#");
								lw.setId(StringUtil.uuid());
								lw.setCreateDate(new Date());
								lw.setStatus(1);
								try{
									lw.setEmployeeId(df.format(Double.parseDouble(StringUtil.nullTrim(row.get(0)))));
								}catch(Exception e){
									lw.setEmployeeId(StringUtil.nullTrim(row.get(0)));
								}
								lw.setEmployeeName(StringUtil.nullTrim(row.get(1)));
								lw.setAllCnt(Float.parseFloat(StringUtil.nullTrimDef(row.get(2),"0")));
								lw.setUseCnt(Float.parseFloat(StringUtil.nullTrimDef(row.get(3),"0")));
								lw.setRestCnt(Float.parseFloat(StringUtil.nullTrimDef(row.get(4),"0")));
								lw.setRestRemark(StringUtil.nullTrim(row.get(5)));
								lw.setRemark(StringUtil.notNull(row.get(6)));
								lw.setLogDate(logDate);
								hrDao.save(lw);
								n++;
							}
						}catch(Exception e){
							logger.error("数据读入异常 ： "+e.getMessage());
						}
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error("休假数据异常： "+e.getMessage());
		}
		return n;
	}

}
